//
//  ZipArchive+BlocksHelper.m
//  MissPaparazzi
//
//  Created by Denis Liger on 4/13/12.
//  Copyright (c) 2012 Pr-App.com. All rights reserved.
//
//  Modified by Alexey Aleshkov on 02.09.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "ZipArchive+BlocksHelper.h"


@implementation ZipArchive (BlocksHelper)

+ (BOOL)unzipFile:(NSString *)zipFilePath destFolder:(NSString *)destFolderPath
{
	BOOL result = NO;

	ZipArchive *zipArchive = [[ZipArchive alloc] init];
	if ([zipArchive UnzipOpenFile:zipFilePath]) {
		result = !![zipArchive UnzipFileTo:destFolderPath overWrite:YES];
		[zipArchive UnzipCloseFile];
	}
	[zipArchive release];

	return result;
}

+ (BOOL)unzipFile:(NSString *)zipFilePath destFolder:(NSString *)destFolderPath password:(NSString *)pass
{
	BOOL result = NO;

	ZipArchive *zipArchive = [[ZipArchive alloc] init];
	if ([zipArchive UnzipOpenFile:zipFilePath Password:pass]) {
		result = !![zipArchive UnzipFileTo:destFolderPath overWrite:YES];
		[zipArchive UnzipCloseFile];
	}
	[zipArchive release];

	if (result) {
		NSFileManager *fileManager = [[NSFileManager alloc] init];
		// Check if file not have 0 bytes length
		NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:destFolderPath error:NULL];
		if ([directoryContent count] > 0) {
			NSString *firstFile = [directoryContent objectAtIndex:0];
			NSString *firstFileFullPath = [destFolderPath stringByAppendingPathComponent:firstFile];
			NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:firstFileFullPath error:NULL];
			unsigned long long fileSize = [fileAttributes fileSize];
			result = fileSize > 0;
			
			if (!result) {
				[fileManager removeItemAtPath:firstFileFullPath error:NULL];
			}
		}
		[fileManager release];
	}

	return result;
}

+ (void)unpackFile:(NSString *)filePath toFolder:(NSString *)folderPath usingPassword:(NSString *)password successBlock:(ZipArchiveUnpackSuccessBlock)successBlock failureBlock:(ZipArchiveUnpackFailureBlock)failureBlock
{
	ZipArchiveUnpackSuccessBlock localSuccessBlock = [successBlock copy];
	ZipArchiveUnpackFailureBlock localFailureBlock = [failureBlock copy];
	
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		BOOL result = [self unzipFile:filePath destFolder:folderPath password:password];
		dispatch_async(dispatch_get_main_queue(), ^{
			if (result) {
				if (localSuccessBlock) {
					localSuccessBlock();
				}
			} else {
				if (localFailureBlock) {
					localFailureBlock();
				}
			}
			[localFailureBlock release];
			[localSuccessBlock release];
		});
	});
}

+ (void)unpackFile:(NSString *)filePath toFolder:(NSString *)folderPath successBlock:(ZipArchiveUnpackSuccessBlock)successBlock failureBlock:(ZipArchiveUnpackFailureBlock)failureBlock
{
	ZipArchiveUnpackSuccessBlock localSuccessBlock = [successBlock copy];
	ZipArchiveUnpackFailureBlock localFailureBlock = [failureBlock copy];
	
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		BOOL result = [self unzipFile:filePath destFolder:folderPath];
		dispatch_async(dispatch_get_main_queue(), ^{
			if (result) {
				if (localSuccessBlock) {
					localSuccessBlock();
				}
			} else {
				if (localFailureBlock) {
					localFailureBlock();
				}
			}
			[localFailureBlock release];
			[localSuccessBlock release];
		});
	});
}

@end
